package cn.legend.travel.admin.content.service.impl;

import cn.legend.travel.admin.content.dao.persist.repository.IBannerRepository;
import cn.legend.travel.admin.content.pojo.param.BannerParam;
import cn.legend.travel.admin.content.pojo.vo.BannerVO;
import cn.legend.travel.admin.content.service.IBannerService;
import cn.legend.travel.common.enumerator.ServiceCode;
import cn.legend.travel.common.exception.ServiceException;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@RequiredArgsConstructor
public class BannerServiceImpl implements IBannerService {
    private final IBannerRepository bannerRepository;

    @Override
    public Map<Integer, List<BannerVO>> getBannerList() {
        List<BannerVO> bannerList = bannerRepository.getBannerList();
        return bannerList.stream().collect(Collectors.groupingBy(BannerVO::getType));
    }

    @Override
    public void update(BannerParam param) {
        if (bannerRepository.update(param) != 1) {
            throw new ServiceException(ServiceCode.ERROR_UPDATE,"服务器忙,请稍后重试");
        }
    }
}
